$(function() {
	$('#photo-hot-loader').fadeIn();
	$('#video-hot-loader').fadeIn();
	$('#joke-hot-loader').fadeIn();
	$.get("/photo-most-view", function(data) {
		$('#photo-hot-loader').fadeOut();
		for ( var i = 0; i < data.length; ++i) {
			var obj = data[i];
			var html = "<div class='photoListItemSmall'>"
			+ "<a href='/photo/" + obj.id +"'>"
			+ "<div class='photo-thumbnail'> <img src='/resources/data/photos/thumbnails/" + obj.url +"'></div>"
			+ "<div class='info'> <h3 class='title'>" + obj.title + "</h3>"
			+ "<div class='stats'> <span class='views-small' title='lượt xem'>" + obj.viewNumber.format()
			+ "</span> <span class='comments-small' title='lượt bình luận'>" + obj.commentNumber.format() + "</span></div></div>"
			+ "<div class='clear'></div> </a> </div>";
			$("#photo-hot").append(html);
		};
		$("#photo-hot").append("<div class='viewMore'><a href='/'>Xem thêm >>></a></div>");
	}, 'json').fail(function(e, data) {
//		alert("error " + data);
	});
	
	$.get("/video-most-view", function(data) {
		$('#video-hot-loader').fadeOut();
		for (var i = 0; i < data.length; ++i) {
			var obj = data[i];
			var html = "<div class='videoListItemSmall'>"
			+ "<a href='/video/" + obj.id + "' target='_blank'>"
			+ "<div class='thumb'> <img src='http://i.ytimg.com/vi/"+ obj.youtubeId + "/" + obj.smallThumbnail +"'><div class='duration'>"+ obj.duration +"</div></div>"
			+ "<div class='info'> <h3 class='title'>" + obj.title + "</h3>"
			+ "<div class='stats'> <span class='views-small' title='lượt xem'>" + obj.viewNumber.format()
			+ "</span> <span class='comments-small' title='lượt bình luận'>" + obj.commentNumber.format()
			+ "</span></div></div><div class='clear'></div> </a> </div>";
			$("#video-hot").append(html);
		};
		$("#video-hot").append("<div class='viewMore'><a href='/video'>Xem thêm >>></a></div>");
	}, 'json').fail(function(e, data) {
//		alert("error " + data);
	});
	
	$.get("/joke-most-view", function(data) {
		$('#joke-hot-loader').fadeOut();
		for (var i = 0; i < data.length; ++i) {
			var obj = data[i];
			var html = "<div class='videoListItemSmall'>"
			+ "<a href='/truyen-cuoi/" + obj.id + "' target='_blank'>"
			+ "<div class='thumb'> <img src='/resources/data/joke-thumbnails/" + obj.image +"'></div>"
			+ "<div class='info'> <h3 class='title'>" + obj.title + "</h3>"
			+ "<div class='stats'> <span class='views-small' title='lượt xem'>" + obj.viewNumber.format()
			+ "</span> <span class='comments-small' title='lượt bình luận'>" + obj.commentNumber.format()
			+ "</span></div></div><div class='clear'></div> </a> </div>";
			$("#joke-hot").append(html);
		};
		$("#joke-hot").append("<div class='viewMore'><a href='/truyen-cuoi'>Xem thêm >>></a></div>");
	}, 'json').fail(function(e, data) {
//		alert("error " + data);
	});
	
	Number.prototype.format = function(n, x) {
	    var re = '\\d(?=(\\d{' + (x || 3) + '})+' + (n > 0 ? '\\.' : '$') + ')';
	    return this.toFixed(Math.max(0, ~~n)).replace(new RegExp(re, 'g'), '$&.');
	};
			
});